import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;

/**
 * @author leejun.happy@gmail.com
 * @date 2017年7月17日 下午1:59:56
 * @describe
 */

public class Shell {
	
	public static final int WIDTH = 5;
	public static final int HEIGHT = 5;
	public static final int SPEED = 10;
	
	private int x, y;
	private Direction direction;
	private boolean live = true;
	public boolean good;
	
	public Shell(int x, int y, Direction direction) {
		this.x = x;
		this.y = y;
		this.direction = direction;
	}
	
	void draw(Graphics g) {
		if (!this.isLive()) {
			TankControl.shells.remove(this);
			return;
		}
		g.setColor(Color.BLACK);
		g.fillOval(this.x, this.y, WIDTH, HEIGHT);
		this.move();
	}
	
	void move() {
		switch (this.direction) {
		case D:
			this.y += SPEED;
			break;
		case L:
			this.x -= SPEED;
			break;
		case U:
			this.y -= SPEED;
			break;
		case R:
			this.x += SPEED;
			break;
		default:
			break;
		}
		
		if (this.x < 0 || this.y < 0 || this.x > TankView.WIDTH || this.y > TankView.HEIGHT) {
			this.live = false;
		}
	}
	
	public Rectangle getRectangel() {
		return new Rectangle(x, y, WIDTH, HEIGHT);
	}
	
	public boolean hitTank(Tank t) {
		if (this.getRectangel().intersects(t.getRectangel()) && t.good != this.good) {
			t.live = false;
			return true;
		}
		return false;
	}
	
	public void hitTanks(List<Tank> tanks) {
		for (int i = 0; i < tanks.size(); i++) {
			Tank t = tanks.get(i);
			boolean b = hitTank(t);
			if (b) {
				tanks.remove(t);
			}
		}
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Direction getDirection() {
		return direction;
	}

	public void setDirection(Direction direction) {
		this.direction = direction;
	}

	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}
	
}
